package service;

import java.util.List;
import dao.User;
import dao.UserDAO;
import dao.Usertemp;

import org.apache.log4j.Logger;

import util.encryption;
import util.sendMail;

import java.security.MessageDigest;

public class UserService {
	private UserDAO userDao;
	public UserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

	public static Logger getLog() {
		return log;
	}

	public static void setLog(Logger log) {
		UserService.log = log;
	}

	public static Logger log = Logger.getLogger(UserService.class);

	public void insertInuser(User user) {
		//List<Usertemp> resultTemp = usertempDao.findByUsername(username);
		userDao.save(user);	
		}
	//业务逻辑层
	public User isValid(String username, String password) throws Exception{
		List<User> resultTemp = userDao.findByUsername(username);
		User user;
		
		encryption secret = new encryption() ;
		
		if (null==resultTemp || resultTemp.size()==0){
			return null;
		}
		else
			{
			//按正常来说就一个位置
				user=resultTemp.get(0);
				//判断用户输入的数据是否和我们数据库中的数据相同
				
				String rnd = user.getCode() ;
				String pass = secret.MD5(password+rnd);
				
				
				if (user.getPassword().equals(pass)){
					return user;
				}
				else
					return null;
			}
	}
	
	public List<User> emailSearch(String email) {
		List<User> resultTemp = userDao.findByEmail(email);
		if (null==resultTemp || resultTemp.size()==0){
			return null;
		}
		else
		{
			return resultTemp;
		}
	}
}
